//
//  XWApiConfig.swift
//  XiaoweHealthy
//
//  Created by Riven on 2022/3/23.
//

import Foundation
import HandyJSON

/// Api 服务环境 类型
enum XWHServiceApiType: Int {
    
    case develop
    case test
    case release
    
    var curUIVersion: String {
        var tText = ""
        switch self {
        case .develop:
            tText = "Develop"
        case .test:
            tText = "Test"
        case .release:
            break
        }
        
        var curVersion = "V" + AppCheckAppVersion.currentVersion
        if tText.isEmpty {
           return curVersion
        }
        
        curVersion += " (\(tText))"
        
        return curVersion
    }
    
    var apiDomain: String {
        switch self {
        case .develop:
            return kDevelopServiceApiDomain
            
        case .test:
            return kTestServiceApiDomain
            
        case .release:
            return kReleaseServiceApiDomain
        }
    }
    
    var apiServiceKey: String {
        switch self {
        case .develop:
            return kDevelopServiceApiKey
            
        case .test:
            return kTestServiceApiKey
            
        case .release:
            return kReleaseServiceApiKey
        }
    }
    
    var apiAESKey: String {
        switch self {
        case .develop:
            return kDevelopAESKey
            
        case .test:
            return kTestAESKey
            
        case .release:
            return kReleaseAESKey
        }
    }
    
}

/// 是否为测试服务器
public var isReleaseService: Bool {
    
    switch serviceApiType {
    case .develop:
        return false
        
    case .test:
        return false
        
    case .release:
        return true
    }
    
}

extension AppDelegate {
    
    static let appVersionNumber = "1.0.14.9"
    
    static let appUrlSchemes = "dashengtingli"
    
    // MARK: - 在 App Store 上的信息
    static let appDownloadDomain = "https://apps.apple.com/app/id"
    static let appStoreAppId = "6476973263"
    static let appDownloadUrl = "\(appDownloadDomain)\(appStoreAppId)"

}


// MARK: - 接口appkey
private(set) var serviceApiType: XWHServiceApiType = .release


//private let kDevelopserviceAipKeyData: [UInt8] = []
private let kDevelopServiceApiKey = ""

private let kTestServiceApiKeyData: [UInt8] = [117, 76, 93, 88, 110, 106, 81, 94, 94, 59, 115, 112, 89, 89, 90, 112, 108, 85, 130, 82, 93, 124, 93, 116, 122, 96, 82, 127, 78, 95, 124, 120]
private var kTestServiceApiKey: String {
    appDataToString(kTestServiceApiKeyData)
}

//private let kTestServiceApiKey = "mDUPfbIVV3khQQRhdMzJUtUlrXJwFWtp"


private let kReleaseServiceApiKeyData: [UInt8] = [84, 128, 123, 107, 98, 79, 59, 114, 95, 95, 82, 109, 122, 130, 119, 115, 111, 122, 98, 58, 81, 86, 123, 63, 83, 62, 121, 77, 91, 82, 96, 89]
private var kReleaseServiceApiKey: String {
    appDataToString(kReleaseServiceApiKeyData)
}

//private let kReleaseServiceApiKey = "LxscZG3jWWJerzokgrZ2INs7K6qESJXQ"

// MARK: - AES 加密密钥
private let kDevelopAESKey = ""

private let kTestAESKeyData: [UInt8] = [77, 93, 105, 81, 74, 78, 89, 107, 75, 91, 56, 122, 77, 93, 93, 123, 64, 97, 73, 77, 127, 127, 69, 69]
private var kTestAESKey: String {
    appDataToString(kTestAESKeyData)
}

//private let kTestAESKey = "EUaIBFQcCS0rEUUs8YAEww=="


private let kReleaseAESKeyData: [UInt8] = [122, 120, 97, 115, 108, 85, 81, 96, 108, 113, 59, 107, 59, 57, 125, 113, 107, 77, 129, 84, 127, 73, 69, 69]
private var kReleaseAESKey: String {
    appDataToString(kReleaseAESKeyData)
}

//private let kReleaseAESKey = "rpYkdMIXdi3c31uicEyLwA=="


// MARK: - 域名
/// 开发环境
// 开发环境管理后台
// http://192.168.1.110:38081

// 使用 这个 域名 https://2g872y0587.oicp.vip 可以访问内网的接口，我这边做了内网穿透功能
private let kDevelopServiceApiDomain = "http://192.168.1.2:9090"

/// 测试环境
//private let kTestServiceApiDomain = "https://test-skysound.xiaowe.cc"
private let kTestServiceApiDomain = "https://test-store.xiaowe.cc"

/// 线上环境
/**
 encrypt-secret: LxscZG3jWWJerzokgrZ2INs7K6qESJXQ 用来进行 header头签名的secret
   aes-key: rpYkdMIXdi3c31uicEyLwA==  用来进行对敏感数据进行aes加密/解密的密钥
 */
private let kReleaseServiceApiDomain = "https://store.xiaowe.cc"

// MARK: - 保存 Api 配置

class XWHApiConfigInfoModel: HandyJSON, CustomStringConvertible {
    
    var _serviceApiType: Int = 2
    var serviceApiType: XWHServiceApiType {
        get {
            XWHServiceApiType(rawValue: _serviceApiType) ?? .release
        }
        set {
            _serviceApiType = newValue.rawValue
        }
    }
    
    var isPrintLog: Bool = false
    
    var description: String {
        "\(String(describing: XWHApiConfigInfoModel.self)) = { serviceApiType = \(serviceApiType), isPrintLog = \(isPrintLog) } \n"
    }
    
    required init() {
        
    }
    
}

private let kApiConfigInfoKey = "xiaowe-api-config-info"

internal func saveApiConfigInfo(configInfo: XWHApiConfigInfoModel?) {
    guard let tInfo = configInfo else {
        log.info("删除 Api 配置信息")
        
        UserDefaults.standard.removeObject(forKey: kApiConfigInfoKey)
        
        return
    }
    
    guard let tJson = tInfo.toJSONString() else {
        return
    }
    
    log.info("保存 Api 配置信息 apiConfigInfo = \(tJson)")
    UserDefaults.standard.set(tJson, forKey: kApiConfigInfoKey)
}

internal func getApiConfigInfo() -> XWHApiConfigInfoModel {
    guard let tJson = UserDefaults.standard.string(forKey: kApiConfigInfoKey) else {
        return XWHApiConfigInfoModel()
    }
    
    guard let tInfo = XWHApiConfigInfoModel.deserialize(from: tJson) else {
        return XWHApiConfigInfoModel()
    }
    
    return tInfo
}

/// 加载 Api 配置信息
internal func setupApiConfigInfo() {
//    let tInfo = getApiConfigInfo()
//
//    log.info("加载 Api 配置信息 info = \(tInfo)")
//    serviceApiType = tInfo.serviceApiType
    
    saveApiConfigInfo(configInfo: nil)
}

// MARK: - --------
//internal func appStringToData(_ tString: String) -> [UInt8] {
//    guard let tdataBytes = tString.data(using: .ascii)?.dataBytes else {
//        return []
//    }
//    var rDataBytes: [UInt8] = []
//    for iByte in tdataBytes {
//        rDataBytes.append(iByte + 8)
//    }
//    
//    return rDataBytes
//}

internal func appDataToString(_ tData: [UInt8]) -> String {
    if tData.isEmpty {
        return ""
    }
    
    var tDataBytes: [UInt8] = []
    
    for iByte in tData {
        tDataBytes.append(iByte - 8)
    }
    
    let rString = Data(tDataBytes).string(encoding: .ascii) ?? ""
    
//    log.debug("appDataToString rString = \(rString) \n")
    
    return rString
}
